logo

List of common Node Variables in node.html.twig in Drupal 10

08
December

List of common Node Variables in node.html.twig in Drupal 10
By: Anonymous | Published On: Sun, 12/08/2024 - 01:38

In Drupal 10, when working with the node.html.twig file, there are several useful variables available for rendering nodes. These variables provide access to the node entity, fields, metadata, and other useful information, making it easier to customize the way content is displayed.

Here’s a list of common Node Variables in node.html.twig in Drupal 10:

Core Node Variables

  1. node:
    • The full node entity object. You can use it to access any properties or fields of the node programmatically.
    • Example: {{ node.title }}
  2. content:
    • The rendered fields of the node. This is an array where each key corresponds to a field, and the value is the rendered HTML.
    • Example: {{ content.body }}, {{ content.field_image }}
  3. label:
    • The title of the node (i.e., node.title).
    • Example: {{ label }}
  4. url:
    • The URL of the node, typically used for linking to the node's full page.
    • Example: <a href="{{ url }}">{{ label }}</a>
  5. bundle:
    • The content type of the node (e.g., article, page).
    • Example: {{ bundle }}
  6. view_mode:
    • The view mode in which the node is being rendered (e.g., full, teaser).
    • Example: {{ view_mode }}
  7. attributes:
    • The HTML attributes applied to the <article> or other node wrapper. This can include classes, IDs, etc.
    • Example: <article{{ attributes }}>
  8. created:
    • The timestamp of when the node was created.
    • Example: {{ node.created|date('F j, Y') }}
  9. changed:
    • The timestamp of when the node was last updated.
    • Example: {{ node.changed|date('F j, Y') }}
  10. author:
    • The user object of the node author, which provides access to user data like the author's name, URL, etc.
    • Example: {{ author.name }}, {{ author.picture }}
  11. uid:
    • The user ID of the node's author.
    • Example: {{ node.uid }}
  12. status:
    • The published status of the node (1 for published, 0 for unpublished).
    • Example: {{ node.status }}
  13. nid:
    • The node ID (unique identifier for the node).
    • Example: {{ node.nid }}
  14. type:
    • The content type of the node (same as bundle).
    • Example: {{ type }}
  15. taxonomy:
    • Taxonomy terms associated with the node (for nodes with term reference fields).
    • Example: {{ content.field_tags }}

Field-Specific Variables

Each field in your content type has its own variable in the content array, typically in the form of content.field_name, where field_name is the machine name of the field.

Examples of field-related variables:

  • content.field_image: The rendered image field.
  • content.field_description: The rendered text field.
  • content.field_tags: The rendered taxonomy reference field.

For example, to display a specific field’s content, you would use:

{% if content.field_image %}

{{ content.field_image }}

{% endif %}

Other Useful Variables

  • teaser: A boolean variable indicating whether the node is being rendered in the teaser view mode (usually for listing pages).
    • Example: {% if teaser %}This is a teaser{% endif %}
  • node_url: The path to the node’s full page (similar to url, but formatted as a path).
    • Example: {{ node_url }}
  • user_picture: The picture of the node’s author (if available).
    • Example: {{ author.picture }}
  • node_language: The language of the node.
    • Example: {{ node.language }}
  • is_front: Boolean indicating if the node is being displayed on the front page.
    • Example:
      {% if is_front %}This is the front page{% endif %}

Need Help ?